package com.k2data.k2app.controller;

import com.k2data.k2app.domain.DeviceCond;
import com.k2data.k2app.domain.po.MonitorLimitValue;
import com.k2data.k2app.domain.po.MonitorPoint;
import com.k2data.k2app.domain.po.MonitorVariable;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.MonitorService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author lidong9144@163.com 17-3-15.
 */
@RestController
@RequestMapping("/monitors")
public class MonitorController {

    private final MonitorService monitorService;

    @Autowired
    public MonitorController(MonitorService monitorService) {
        this.monitorService = monitorService;
    }

    @ApiOperation(value = "添加测点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monitorPoint", value = "monitorPoint", required = true, dataType = "MonitorPoint", paramType = "body")
    })
    @PostMapping(value = "/points", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> postMonitorPoint(@Valid @RequestBody MonitorPoint monitorPoint) throws Exception {
        return ResponseHelper.success(monitorService.insertPoint(monitorPoint));
    }

    @ApiOperation(value = "更新测点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "monitorPoint", value = "monitorPoint", required = true, dataType = "MonitorPoint", paramType = "body")
    })
    @PutMapping(value = "/points/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> putMonitorPoint(@PathVariable("id") Long id, @RequestBody MonitorPoint monitorPoint) throws Exception {
        monitorPoint.setId(id);

        return ResponseHelper.success(monitorService.updatePoint(monitorPoint));
    }

    @ApiOperation(value = "删除测点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    })
    @DeleteMapping(value = "/points/{id}")
    public ResponseEntity<CommonResultResponse> deleteMonitorPoint(@PathVariable("id") Long id) throws Exception {
        return ResponseHelper.success(monitorService.deletePoint(id));
    }

    @ApiOperation(value = "添加监控指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monitorValue", value = "monitorValue", required = true, dataType = "MonitorVariable", paramType = "body")
    })
    @PostMapping(value = "/variables", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> postMonitorValue(@Valid @RequestBody MonitorVariable monitorValue) throws Exception {
        return ResponseHelper.success(monitorService.insertVariable(monitorValue));
    }

    @ApiOperation(value = "更新监控指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "monitorValue", value = "monitorValue", required = true, dataType = "MonitorVariable", paramType = "body")
    })
    @PutMapping(value = "/variables/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> putMonitorValue(@PathVariable("id") Long id, @RequestBody MonitorVariable monitorValue) throws Exception {
        monitorValue.setId(id);

        return ResponseHelper.success(monitorService.updateVariable(monitorValue));
    }

    @ApiOperation(value = "删除监控指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    })
    @DeleteMapping(value = "/variables/{id}")
    public ResponseEntity<CommonResultResponse> deleteMonitorValue(@PathVariable("id") Long id) throws Exception {
        return ResponseHelper.success(monitorService.deleteVariable(id));
    }

    @ApiOperation(value = "添加阀值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limitValue", value = "limitValue", required = true, dataType = "MonitorLimitValue", paramType = "body")
    })
    @PostMapping(value = "/limits", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> postMonitorPoint(@Valid @RequestBody MonitorLimitValue limitValue) throws Exception {
        return ResponseHelper.success(monitorService.insertLimit(limitValue));
    }

    @ApiOperation(value = "更新阀值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "limitValue", value = "limitValue", required = true, dataType = "MonitorLimitValue", paramType = "body")
    })
    @PutMapping(value = "/limits/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> putMonitorPoint(@PathVariable("id") Long id, @RequestBody MonitorLimitValue limitValue) throws Exception {
        limitValue.setId(id);

        return ResponseHelper.success(monitorService.updateLimit(limitValue));
    }

    @ApiOperation(value = "删除阀值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    })
    @DeleteMapping(value = "/limits/{id}")
    public ResponseEntity<CommonResultResponse> putMonitorPoint(@PathVariable("id") Long id) throws Exception {
        return ResponseHelper.success(monitorService.deleteLimit(id));
    }

    @ApiOperation(value = "设备测点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "deviceId", dataType = "Long", paramType = "query")
    })
    @GetMapping(value = "/points")
    public ResponseEntity<CommonResultResponse> getDevicePoints(@RequestParam(value = "deviceId", required = false) Long deviceId) throws Exception {
        DeviceCond cond = new DeviceCond();
        cond.setDeviceId(deviceId);

        return ResponseHelper.success(monitorService.selectPoints(cond));
    }

    @ApiOperation(value = "设备测点变量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "deviceId", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "monitorPointId", value = "monitorPointId", dataType = "Long", paramType = "query")
    })
    @GetMapping(value = "/variables")
    public ResponseEntity<CommonResultResponse> getDeviceVariables(
            @RequestParam(value = "deviceId", required = false) Long deviceId,
            @RequestParam(value = "monitorPointId", required = false) Long monitorPointId) throws Exception {
        DeviceCond cond = new DeviceCond();
        cond.setDeviceId(deviceId);
        cond.setMonitorPointId(monitorPointId);

        return ResponseHelper.success(monitorService.selectVariables(cond));
    }

    @ApiOperation(value = "设备测点阈值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "deviceId", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "monitorPointId", value = "monitorPointId", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "variableId", value = "variableId", dataType = "Long", paramType = "query")
    })
    @GetMapping(value = "/limits")
    public ResponseEntity<CommonResultResponse> getDeviceLimitValues(
            @RequestParam(value = "deviceId", required = false) Long deviceId,
            @RequestParam(value = "monitorPointId", required = false) Long monitorPointId,
            @RequestParam(value = "variableId", required = false) Long variableId) throws Exception {
        DeviceCond cond = new DeviceCond();
        cond.setDeviceId(deviceId);
        cond.setMonitorPointId(monitorPointId);
        cond.setVariableId(variableId);

        return ResponseHelper.success(monitorService.selectLimits(cond));
    }

}
